﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using System.Data.SqlClient;
using System.Data;

namespace BLL
{
    public class TIENCANBLL
    {
        DataService DB = new DataService();

        public void insert(string tiencan, int mabenhan, DateTime ngaytao, string file_patch){
            string sql = "insert into tiencan values (@tiencan,@mabenhan,@ngaytao,@file_patch)";
            SqlParameter paraTiencan = new SqlParameter("tiencan", tiencan);
            SqlParameter paraMaba = new SqlParameter("mabenhan", mabenhan);
            SqlParameter paraNgay = new SqlParameter("ngaytao", ngaytao);
            SqlParameter paraFilePatch = new SqlParameter("file_patch", file_patch);

            DB.ExecuteNonQuery(sql, paraTiencan, paraMaba, paraNgay, paraFilePatch);
        }

        public void update(int matiencan, string tiencan, int mabenhan, DateTime ngaytao, string file_patch)
        {
            string sql = "update tiencan set tiencan = N'" + tiencan + ", mabenhan = '" + mabenhan.ToString() + "', ngaytao = '" + ngaytao.ToString() + "' , file_patch = '" + file_patch + "' where matiencan = " + matiencan.ToString();
            DB.ExecuteNonQuery(sql);
        }

        public void delete(int matiencan)
        {
            string sql = "delete from tiencan where matiencan = " + matiencan.ToString();
            DB.ExecuteNonQuery(sql);
        }

        public TIENCAN getTiencanWithIDBaiviet(int idBaiviet) {
            string sql = "select tc.matiencan, tc.tiencan, tc.mabenhan, tc.ngaytao, tc.file_patch " +
                         " from hosobenhan hsba,baiviet bv,tiencan tc where bv.mabaiviet = hsba.mabaiviet " +
                            " and tc.mabenhan = hsba.mabenhan and bv.mabaiviet = " + idBaiviet.ToString();

            DataTable table = new DataTable();
            table = DB.ExecuteReader(sql);

            TIENCAN tc = new TIENCAN();
            DataRow r = table.Rows[0];

            tc.matiencan = Int32.Parse( r[0].ToString());
            tc.tiencan = r[1].ToString();
            tc.mabenhan = Int32.Parse( r[2].ToString());
            tc.ngaytao = Convert.ToDateTime(r[3].ToString());
            try { tc.file_patch = r[4].ToString();}
            catch { tc.file_patch = ""; }

            return tc;
        }

        public void updateTiencanContentWithId(int idTiencan, string tiencan) {
            string sql = "update tiencan set tiencan = N'" + tiencan + "' where matiencan = " + idTiencan.ToString();
            DB.ExecuteNonQuery(sql);
        }   
    }
}
